package Dao

import (
	"fmt"
	"zlxBook/db"
	"zlxBook/model"
)

func AddCategory(c *model.Category) error {
	result := db.DB.Create(c)
	if result.Error != nil {
		fmt.Println("添加种类出错了")
		return result.Error
	}
	return nil
}
func UpdateCategory(c *model.Category) error {
	result := db.DB.Save(&c)
	if result.Error != nil {
		fmt.Println("添加文件出错了")
		return result.Error
	}
	return nil
}
func DeleteCategoryById(id int) error {
	category := &model.Category{}
	err := db.DB.Where("id = ?", id).Take(&category).Error
	if err != nil {
		fmt.Println("查询种类错误")
		return err
	}
	err2 := db.DB.Delete(&category).Error
	if err2 != nil {
		fmt.Println("删除种类错误")
		return err
	}
	return nil
}

//查询全部数据
func GetCategories() ([]*model.Category, error) {
	categories := []*model.Category{}
	err := db.DB.Find(&categories).Error
	if err != nil {
		return nil, err
	}
	return categories, nil
}
